<?php
class MUser extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }

	function verifyUser($u,$pw, $branch){
		$this->db->select('us,ps,branch,group');
		$this->db->where('us',db_clean($u,50));
		$this->db->where('ps', db_clean(md5($pw)));
        $this->db->where('branch', db_clean($branch));
		$this->db->where('active', '1');
		$this->db->limit(1);
		$Q = $this->db->get('c_users');

		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			$_SESSION['username'] = $row['us'];
            $_SESSION['branch'] = $row['branch'];
            $_SESSION['group'] = $row['group'];
		}else{
			 $_SESSION['username'] = null; 
			 $_SESSION['branch'] = null;
             $_SESSION['group'] = null;
			$this->session->set_flashdata('error', 'Sorry, your username or password is incorrect!');
		}		
	}
    
	function getDropdownRoles() {
		 $data = array();
         $this->db->order_by("role_id");
	     $Q = $this->db->get('role');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['role_id']] = $row['role'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 		
	}
    
    function getUserList()
    {
        $data = array();
        $this->db->select('user.user_id, user.username, user.activate, user.role_id, role.role');
        $this->db->from('user');
        $this->db->join('role', 'user.role_id = role.role_id');
        $Q = $this->db->get();
        if ($Q->num_rows() > 0){
          foreach ($Q->result_array() as $row){
            $data[] = $row;
          }
        }
        $Q->free_result();    
        return $data; 		
    }
    
	function getUserById($id) {
		$data = array();
	    $this->db->where('user_id',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('user');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;      
	}
	
	function addEditUser($id = 0){
        $data = array('username' => db_clean($_POST['username']),
                    'email' => db_clean($_POST['username']),
      				'phone' => db_clean($_POST['phone']),
                    'role_id'=>id_clean(isset($_POST['role_id']) ? $_POST['role_id'] : $_SESSION['role_id']),
                    'activate' => db_clean(isset($_POST['activate']) ? 1 : 0)
                    );
        
       if($id > 0)
       {
           if($_POST['password'] != null && $_POST['password'] != '')
           {
            $data['password'] = db_clean(do_hash($_POST['password']),16);
           }
    	   $this->db->set('modified', 'NOW()', FALSE);
           $this->db->where('user_id',id_clean($id));
    	   $this->db->update('user',$data);
       }
       else
       {
           $data['password'] = db_clean(do_hash($_POST['password']),16);
    	   $this->db->set('created', 'NOW()', FALSE);
    	   $this->db->insert('user',$data);
       }
	}
	
	function deleteUser($id){
 	    $this->db->where('user_id', id_clean($id));
        $this->db->delete('user');
	}
}


?>